import Vue from 'vue'
import Router from "vue-router"

import home from '@/views/home'
import login from '@/views/login'
import Right from '@/views/power/Right'
import Roles from "@/views/power/Roles"
import Category from '@/views/goods/Category'
import Params from '@/views/goods/Params'

Vue.use(Router)

const routes = [
	{ 
		path: '/', 
		redirect: '/login' 
	},
	{ 
		path: '/login', 
		component: login 
	},
	{ 
		path: '/home', 
		component: home,  
		redirect: '/welcome',
		children:[
			{
				path:'/welcome', 
				component: ()=>import('@/views/home/Welcome')
			},{
				path:'/users',
				component: ()=> import('@/views/home/users')
			},{
				path: '/rights',
				component: Right
			},{
				path: '/roles',
				component: Roles
			},{
				path: '/categories',
				component: Category
			},{
				path: '/params',
				component: Params
			}
		] 
	},
]

const router = new Router({
	routes,
	mode: 'history'
})
router.beforeEach((to, from, next) => {
	//如果用户访问登录页，直接放行
	if (to.path === '/login') return next()
	//从sessionStorage中获取到保存的token值
	const token = window.sessionStorage.getItem('token')
	//没有token,强制跳转到登录页
	if (!token) return next('/login')
	next()
})
export default router